function promisefyTween(tw: egret.Tween) {
	return new Promise((resolve, reject) => {
		tw.call(resolve);
	});
}

class DlgStory extends PopupPanel {
	constructor() {
		super();
		this.skinName = 'storySkin';
		fullAlign(this);
	}

	override childrenCreated(): void {
		super.childrenCreated();
	}

	async setStory(story: string, time: number, time2: number) {
		const capters = story.split(';');
		for (const capter of capters) {
			await this.playCapter(capter, time);
			await waitMs(time2);
		}
		this.close();
	}

	l0: eui.Label;

	l1: eui.Label;

	l2: eui.Label;

	async playCapter(capter: string, time: number) {
		const words = capter.split(',');

		const lbs = [this.l0, this.l1, this.l2];
		const xto: number[] = [];
		for (const l of lbs) {
			l.text = '';
		}

		async function playWord(lb: eui.Label, word: string) {
			// lb.text = word;
			// lb.x = (this.width - lb.textWidth)/2;
			// const times = 6;
			for (let i = 0; i < word.length; i++) {
				lb.text = word.substr(0, i + 1);
				await waitMs(time);
			}
		}

		const start = this.l2.x - 100;

		function playWord2(lb: eui.Label, word: string) {
			// const lb = lbs[i];
			lb.text = word;
			const old = lb.x;
			lb.x = start;
			return egret.Tween.get(lb)
				.to({ x: old }, 100)
				.to({ scaleX: 1.5, scaleY: 1.5 }, time)
				.to({ scaleX: 1, scaleY: 1 }, time);
		}

		function playWord3(lb: eui.Label, word: string) {
			// const lb = lbs[i];
			lb.text = word;
			const old = lb.x;
			lb.x = start;
			// lb.x = old;
			return new Promise((resolve, reject) => {
				egret.Tween.get(lb)
					.to({ x: old, scaleX: 1.5, scaleY: 1.5 }, time)
					.to({ scaleX: 1, scaleY: 1 }, time)
					.call(resolve);
			});
		}

		let index = 0;
		for (const word of words) {
			// await promisefyTween(playWord2(lbs[index++], word));
			await playWord3(lbs[index++], word);
			await waitMs(time);
		}
	}
}
